<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>我的订单 - Steam游戏商城</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
    <style>
        :root {
            --primary-color: #1b2838;
            --secondary-color: #2a475e;
            --accent-color: #66c0f4;
            --text-color: #c7d5e0;
            --light-bg: #223040;
            --dark-bg: #171a21;
            --button-color: #5c7e10;
            --button-hover: #79a816;
            --danger-color: #ff6b6b;
            --danger-hover: #ff3838;
        }

        body {
            background-color: var(--primary-color);
            color: var(--text-color);
            font-family: 'Motiva Sans', sans-serif;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 15px;
        }

        .page-title {
            color: white;
            font-size: 28px;
            margin: 30px 0;
            padding-bottom: 10px;
            border-bottom: 1px solid rgba(102, 192, 244, 0.2);
        }

        .empty-orders {
            background-color: var(--light-bg);
            border-radius: 5px;
            padding: 40px;
            text-align: center;
            margin: 20px 0;
        }

        .empty-orders-icon {
            font-size: 60px;
            color: rgba(102, 192, 244, 0.5);
            margin-bottom: 20px;
        }

        .empty-orders-title {
            color: white;
            font-size: 24px;
            margin-bottom: 10px;
        }

        .empty-orders-text {
            color: var(--text-color);
            margin-bottom: 20px;
        }

        .empty-orders-btn {
            background-color: var(--button-color);
            color: white;
            padding: 10px 20px;
            border-radius: 3px;
            text-decoration: none;
            transition: background-color 0.2s;
        }

        .empty-orders-btn:hover {
            background-color: var(--button-hover);
            color: white;
        }

        .order-card {
            background-color: var(--light-bg);
            border-radius: 5px;
            margin-bottom: 20px;
            overflow: hidden;
            transition: transform 0.2s, box-shadow 0.2s;
        }

        .order-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 0 10px rgba(102, 192, 244, 0.1);
        }

        .order-header {
            background-color: var(--secondary-color);
            padding: 15px 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .order-id {
            color: white;
            font-size: 20px;
            font-weight: bold;
        }

        .order-status {
            padding: 5px 10px;
            border-radius: 3px;
            font-size: 14px;
            font-weight: bold;
        }

        .status-paid {
            background-color: rgba(76, 209, 55, 0.2);
            color: #4cd137;
        }

        .status-cancelled {
            background-color: rgba(255, 107, 107, 0.2);
            color: var(--danger-color);
        }

        .order-details {
            padding: 20px;
        }

        .order-info {
            margin-bottom: 15px;
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
        }

        .info-item {
            flex: 1;
            min-width: 200px;
        }

        .info-label {
            color: rgba(199, 213, 224, 0.7);
            font-size: 14px;
        }

        .info-value {
            color: white;
            font-size: 16px;
        }

        .order-items {
            width: 100%;
            margin-top: 15px;
        }

        .order-items th {
            color: rgba(199, 213, 224, 0.7);
            font-weight: normal;
            padding: 10px 0;
            border-bottom: 1px solid rgba(102, 192, 244, 0.1);
        }

        .order-items td {
            padding: 15px 0;
            border-bottom: 1px solid rgba(102, 192, 244, 0.05);
        }

        .order-items tr:last-child td {
            border-bottom: none;
        }

        .game-name {
            color: white;
            font-weight: 500;
        }

        .game-price, .game-subtotal {
            color: var(--accent-color);
            font-weight: bold;
        }

        .total-row {
            font-weight: bold;
            color: white;
        }

        .order-actions {
            margin-top: 15px;
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }

        .action-btn {
            padding: 8px 15px;
            border-radius: 3px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.2s, transform 0.2s;
        }

        .action-btn:hover {
            transform: translateY(-1px);
        }

        .btn-view {
            background-color: var(--secondary-color);
            color: var(--text-color);
        }

        .btn-view:hover {
            background-color: rgba(42, 71, 94, 0.8);
            color: white;
        }

        .btn-delete {
            background-color: rgba(255, 107, 107, 0.2);
            color: var(--danger-color);
        }

        .btn-delete:hover {
            background-color: rgba(255, 56, 56, 0.3);
            color: var(--danger-hover);
        }

        /* 确认对话框 */
        .confirm-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.7);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
            opacity: 0;
            visibility: hidden;
            transition: opacity 0.3s, visibility 0.3s;
        }

        .confirm-modal.active {
            opacity: 1;
            visibility: visible;
        }

        .confirm-content {
            background-color: var(--secondary-color);
            border-radius: 5px;
            padding: 25px;
            max-width: 400px;
            width: 100%;
            transform: scale(0.8);
            transition: transform 0.3s;
        }

        .confirm-modal.active .confirm-content {
            transform: scale(1);
        }

        .confirm-title {
            color: white;
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 15px;
        }

        .confirm-message {
            color: var(--text-color);
            margin-bottom: 20px;
        }

        .confirm-buttons {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
        }

        .confirm-btn {
            padding: 8px 15px;
            border-radius: 3px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.2s;
        }

        .btn-cancel {
            background-color: var(--light-bg);
            color: var(--text-color);
        }

        .btn-cancel:hover {
            background-color: rgba(34, 48, 64, 0.8);
        }

        .btn-confirm {
            background-color: var(--danger-color);
            color: white;
        }

        .btn-confirm:hover {
            background-color: var(--danger-hover);
        }
    </style>
</head>
<body>
<div th:replace="~{fragments/header}"></div>

<div class="container mt-4">
    <h1 class="page-title">我的订单</h1>

    <div th:if="${#lists.isEmpty(orders)}" class="empty-orders">
        <i class="fa fa-shopping-bag empty-orders-icon"></i>
        <h3 class="empty-orders-title">您还没有任何订单</h3>
        <p class="empty-orders-text">浏览我们的游戏库，购买您喜欢的游戏</p>
        <a th:href="@{/games}" class="empty-orders-btn">
            <i class="fa fa-gamepad mr-2"></i>浏览游戏
        </a>
    </div>

    <div th:unless="${#lists.isEmpty(orders)}">
        <div class="order-card" th:each="order : ${orders}">
            <div class="order-header">
                <div class="order-id">订单 #<span th:text="${order.id}">123</span></div>
                <div class="order-status status-paid">已支付</div>
            </div>

            <div class="order-details">
                <div class="order-info">
                    <div class="info-item">
                        <div class="info-label">订单日期</div>
                        <div class="info-value" th:text="${#temporals.format(order.orderDate, 'yyyy-MM-dd HH:mm')}">2023-01-01 12:00</div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">支付方式</div>
                        <div class="info-value">Steam钱包</div>
                    </div>
                    <div class="info-item">
                        <div class="info-label">总金额</div>
                        <div class="info-value game-price" th:text="${'¥' + #numbers.formatDecimal(order.totalAmount, 1, 2)}">¥100.00</div>
                    </div>
                </div>

                <table class="order-items">
                    <thead>
                    <tr>
                        <th>游戏</th>
                        <th>数量</th>
                        <th>单价</th>
                        <th>小计</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="entry : ${order.items}">
                        <td><span class="game-name" th:text="${entry.key.name}">游戏名称</span></td>
                        <td th:text="${entry.value}">1</td>
                        <td class="game-price" th:text="${'¥' + #numbers.formatDecimal(entry.key.price, 1, 2)}">¥100.00</td>
                        <td class="game-subtotal" th:text="${'¥' + #numbers.formatDecimal(entry.key.price * entry.value, 1, 2)}">¥100.00</td>
                    </tr>
                    <tr class="total-row">
                        <td colspan="3" class="text-right">总计:</td>
                        <td class="game-price" th:text="${'¥' + #numbers.formatDecimal(order.totalAmount, 1, 2)}">¥100.00</td>
                    </tr>
                    </tbody>
                </table>

                <div class="order-actions">
                    <button class="action-btn btn-view">查看详情</button>
                    <button class="action-btn btn-delete" th:data-order-id="${order.id}">
                        <i class="fa fa-trash mr-1"></i> 删除订单
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<div th:replace="~{fragments/footer}"></div>

<!-- 确认对话框 -->
<div id="confirmModal" class="confirm-modal">
    <div class="confirm-content">
        <div class="confirm-title">确认删除订单</div>
        <div class="confirm-message">您确定要删除这个订单吗？此操作无法撤销。</div>
        <div class="confirm-buttons">
            <button id="cancelDelete" class="confirm-btn btn-cancel">取消</button>
            <button id="confirmDelete" class="confirm-btn btn-confirm">确认删除</button>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const deleteButtons = document.querySelectorAll('.btn-delete');
        const confirmModal = document.getElementById('confirmModal');
        const cancelDeleteBtn = document.getElementById('cancelDelete');
        const confirmDeleteBtn = document.getElementById('confirmDelete');
        let currentOrderId = null;

        // 显示确认对话框
        deleteButtons.forEach(button => {
            button.addEventListener('click', function() {
                currentOrderId = this.getAttribute('data-order-id');
                confirmModal.classList.add('active');
            });
        });

        // 取消删除
        cancelDeleteBtn.addEventListener('click', function() {
            confirmModal.classList.remove('active');
            currentOrderId = null;
        });

        // 点击背景关闭对话框
        confirmModal.addEventListener('click', function(e) {
            if (e.target === confirmModal) {
                confirmModal.classList.remove('active');
                currentOrderId = null;
            }
        });

        // 确认删除订单
        confirmDeleteBtn.addEventListener('click', function() {
            if (currentOrderId) {
                deleteOrder(currentOrderId);
                confirmModal.classList.remove('active');
            }
        });

        // 删除订单的AJAX请求 - 修改为POST方式
        function deleteOrder(orderId) {
            const orderCard = document.querySelector(`.btn-delete[data-order-id="${orderId}"]`).closest('.order-card');
            orderCard.style.opacity = '0.7';

            // 创建表单数据
            const formData = new FormData();
            formData.append('orderId', orderId);

            // 发送POST请求
            fetch(`/order/delete`, {
                method: 'POST',
                body: formData
            })
                .then(response => {
                    if (response.ok) {
                        // 成功删除，从DOM中移除订单卡片
                        orderCard.style.height = `${orderCard.offsetHeight}px`;
                        orderCard.style.overflow = 'hidden';

                        setTimeout(() => {
                            orderCard.style.transition = 'all 0.3s ease-out';
                            orderCard.style.opacity = '0';
                            orderCard.style.height = '0';
                            orderCard.style.marginBottom = '0';
                            orderCard.style.padding = '0';

                            setTimeout(() => {
                                orderCard.remove();

                                // 如果没有订单了，刷新页面显示空状态
                                const remainingOrders = document.querySelectorAll('.order-card');
                                if (remainingOrders.length === 0) {
                                    window.location.reload();
                                }
                            }, 300);
                        }, 100);
                    } else {
                        // 删除失败，显示错误提示
                        alert('删除订单失败，请重试');
                        orderCard.style.opacity = '1';
                    }
                })
                .catch(error => {
                    console.error('Error deleting order:', error);
                    alert('删除订单时发生错误');
                    orderCard.style.opacity = '1';
                });
        }
    });
</script>
</body>
</html>